<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_SHOP,$login['staff_auth']);

$dao = new Class_M_SHOP;
$media = Class_M_OTHER::get_pattern_array(NULL, 23);
$login_auth = $login['staff_auth'];

if ($login_auth == 'G-2' ) {
	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	
	$forms["sort"] = '3';
	$cellsort = array();
	$zone_cd = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 3);
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 2);
	$zone_cd = array();
}
if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		if  ( !empty( $forms["lnews1"] ) ){
			$forms["lnews1_name"] = $media[$forms["lnews1"]];
		} 
		if  ( !empty( $forms["lnews2"] ) ){
			$forms["lnews2_name"] = $media[$forms["lnews2"]];
		} 
		if  ( !empty( $forms["lnews3"] ) ){
			$forms["lnews3_name"] = $media[$forms["lnews3"]];
		} 
		if  ( !empty( $forms["lnews4"] ) ){
			$forms["lnews4_name"] = $media[$forms["lnews4"]];
		} 
		if  ( !empty( $forms["lnews5"] ) ){
			$forms["lnews5_name"] = $media[$forms["lnews5"]];
		} 
		/*if ($forms["parent_shop_cd"] ) {
			$pdata = $dao->get( $forms["parent_shop_cd"]  );
			if ($pdata) {
				$forms["parent_shop_name"] = $pdata["shop_name"];	
			}
		}
		*/
	}else{
		//$forms['auth'] = 0;
	}
}else{
	$forms = $_POST;

	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		$lnews = array( "lnews1"=>"地域新聞1" , "lnews2"=>"地域新聞2", "lnews3"=>"地域新聞3", "lnews4"=>"地域新聞4", "lnews5"=>"地域新聞5" );
		
		foreach ($lnews as $k=>$v) {
			if (!empty($forms[$k])) {
				foreach ($lnews as $kk=>$vv) {
					if ( ($forms[$k] == $forms[$kk]) && ($k != $kk) ) {
						$err_mes[$k] = $err_mes[$k]."<p class='error'>  {$vv}と重複している </p";
						$err->clear = false;
					}
				}
			}
		}
		//if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
		if( $err_mes['shop_cd'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" shop_cd = '%s'", $forms['shop_cd'] );
			}else{
				$where = sprintf(" shop_cd = '%s' AND id <> %s", $forms['shop_cd'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['shop_cd'] = '<p class="error">このIDは重複しています</p>';
			}
		}
		
		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			$values["year_sale"]         =  empty($forms["year_sale"]) ? 0 : $forms["year_sale"];
			foreach($lnews as $kn => $vn) {
				$values[$kn] = empty($values[$kn])? '0' :$values[$kn];
			}
			
			//$values["media_cd"]      =  Class_M_OTHER::do_media($values["media_name"]);
			$values["stop_date"]       = Class_PWD::date_to_int8 ($values['stop_date']) ;
			

			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
			$err_mes["shop_cd"] =$err->check_size_equal($values["shop_cd"], 3);
			
		}
		
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);
	$smarty->assign('block', $block );
	
	$local= Class_M_OTHER::get_local_array(PLEASE_SELECT_ARR);
	$smarty->assign('local', $local );
	
	//$localnews = Class_M_OTHER::get_local_news_array(PLEASE_SELECT_ARR);
	//$smarty->assign('localnews', $localnews );

}

	if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
	if( isset($areauser) )    $smarty->assign('areauser', $areauser);
$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mshop_form.html');


?>